package cn.ymz.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ymz.model.Urls;
import cn.ymz.observe.Observe;
import cn.ymz.observe.Subject;
import cn.ymz.service.CallService;
import cn.ymz.service.LoginService;

import java.util.*;

public class CallServiceImpl implements CallService {

    private final static CallService INSTANCE = new CallServiceImpl();

    private static final Log log = LogFactory.get();

    private Urls urls = Urls.getInstance();


    private CallServiceImpl() {
    }

    public static CallService getInstance() {
        return INSTANCE;
    }

    @Override
    public List<Map<String, Object>> getCallType() {
        List<Map<String, Object>> maps = new ArrayList<>();
        JSONObject userInfo = LoginService.USER_MAP.get("userInfo");
        if (userInfo != null) {
            String token = userInfo.getStr("token");
            String res = HttpRequest.post(urls.getCallTypeUrl())
                    .form("token", token)
                    .execute()
                    .body();
            log.info("获取拨打方式：" + res);
            JSONObject callTypes = JSONUtil.parseObj(res);
            if ("200".equals(callTypes.getStr("code"))) {
                JSONArray jsonArray = callTypes.getJSONObject("data").getJSONArray("data");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Map<String, Object> resMap = jsonObject.toBean(new TypeReference<HashMap<String, Object>>() {
                    });
                    maps.add(resMap);
                }
            }
        }
        CALL_TYPES.clear();
        CALL_TYPES.addAll(maps);
        return maps;
    }

    @Override
    public void call(String number) {
        Map<String, JSONObject> userMap = LoginService.USER_MAP;
        FileReader reader = new FileReader("CallType.lmt");
        String type = reader.readString();
        if (userMap.size() != 0) {
            JSONObject userInfo = userMap.get("userInfo");
            String token = userInfo.getStr("token");
            Map<String, Object> map = new HashMap<>();
            map.put("token", token);
            map.put("mobile", number);
            map.put("type", type);
            log.info("拨号参数：" + map);
            String body = HttpRequest.post(urls.getCallUrl())
                    .form(map)
                    .execute()
                    .body();
            log.info("拨打结果：" + body);
            JSONObject res = JSONUtil.parseObj(body);
            Subject subject = Subject.getInstance();
            String log = "呼叫时间：" + DateUtil.format(new Date(), "yyyy-MM-dd hh:mm:ss") + "\n" + "呼叫号码：" + number + "\n" + "拨打结果：" + res.get("msg");
            subject.addLog(log);
        }
    }


}
